<template>
  <view class="container">
    <view class="content-title" v-show="playList.length>=1? false : true">歌荒啦，快去添加吧~</view>
    <scroll-view class="content-playlist" scroll-y>
      <view class="playlist-item" v-for="(item, i) in playList" :key="i">
        <view class=".playlist-item-left" @click="musicPlay(i)">
          <image class="playlist-cover" :src="item.imgUrl"></image>
          <view class="playlist-info">
            <view class="playlist-info-title">{{item.title}}</view>
            <view class="playlist-info-singer">{{item.singer}}</view>
          </view>
          <view class="playlist-controls">
            <text v-if="playInfo.title === playList[i].title">正在播放</text>
          </view>
        </view>
        <uni-icons class="playlist-item-icon" type="closeempty" size="18" color="#ddd"
          v-if="playInfo.title !== playList[i].title" @click="deletePlayListItem(item)"></uni-icons>
      </view>
    </scroll-view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    name: "music-playlist",
    computed: {
      ...mapState('m_music', ['playList', 'playInfo', 'audioState', 'playIndex', 'audioCtx'])
    },
    data() {
      return {

      };
    },
    methods: {
      ...mapMutations('m_music', ['setState', 'setMusic', 'audioWait', 'updateItem', 'deletePlayList']),
      // 音乐播放事件
      musicPlay(e) {
        this.updateItem(1)
        if (this.audioState === 'running' && this.playIndex === e) {
          return
        }
        this.setMusic(e)
        this.audioCtx.play()
        this.setState('running')
        this.audioWait()
      },
      // 删除播放列表歌曲事件
      deletePlayListItem(musicItem) {
        this.deletePlayList(musicItem)
      }
    }
  }
</script>

<style lang="scss">
  .container {
    height: calc(100% - 82rpx);

    .content-title {
      font-size: 12pt;
      color: #ccc;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    .content-playlist {
      height: calc(100% - 1rpx);

      .playlist-item {
        width: 100%;
        display: flex;
        align-items: center;
        border-bottom: 1rpx solid #ddd;
        height: 112rpx;

        .playlist-item-left {
          width: 100%;
          display: flex;
          align-items: center;
        }

        .playlist-item-icon {
          margin-right: 20rpx;
        }
      }

      .playlist-cover {
        width: 80rpx;
        height: 80rpx;
        margin-left: 30rpx;
        border-radius: 8rpx;
      }

      .playlist-info {
        flex: 1;
        font-size: 10pt;
        line-height: 38rpx;
        margin-left: 20rpx;

        .playlist-info-singer {
          color: #888;
        }
      }

      .playlist-controls {
        font-size: 10pt;
        margin-right: 20rpx;
        color: #fba007;
      }
    }
  }
</style>